package readFromDB;
import java.util.List;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import classes.Courses;
import classes.DB;

public class GetCourse {
	  Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;
      String sql="";
      List<Courses> courseList;
     // 读取全部课程
	public List<Courses> getByTableName(String tablename) throws SQLException {
		try { 
			courseList=new ArrayList<Courses>();
			conn=DB.GetConnection();
			stmt =conn.createStatement();
			sql="select * from "+tablename;
			rs=stmt.executeQuery(sql);
			while(rs.next()){
				Courses tempCourses=new Courses();
				tempCourses.setCuoChoesd(rs.getInt("selperson"));
				tempCourses.setCuoTeacher(rs.getString("teacher"));
				tempCourses.setCuoId(rs.getString("Cno"));
				tempCourses.setCuoLimmit(rs.getInt("plim"));
				tempCourses.setCuoName(rs.getString("Cname"));
				tempCourses.setCuoPosition(rs.getString("Cloc"));
				tempCourses.setCuoTime(rs.getString("Ctime"));
				tempCourses.setCuoPro(rs.getString("Cpno"));
				courseList.add(tempCourses);
			}
			
	         rs.close();
	         stmt.close();
	         conn.close();
	     } 
	    
	     catch (SQLException e) {
	         e.printStackTrace();
	         System.out.println("无法连接数据库");
	     }
	     catch(Exception e){
	         // 处理 Class.forName 错误
	         e.printStackTrace();
	       }
		return courseList;
	}
	//判断之前有没有选择课程
	public boolean isChosed(String stuId) throws SQLException {
		boolean chosed=false;
		try{
			sql="select * from "+stuId+"_courses";
			conn=DB.GetConnection();
			stmt =conn.createStatement();
			rs=stmt.executeQuery(sql);
			if(rs.next()) {
				chosed=true;
			}
	         rs.close();
	         stmt.close();
	         conn.close();
		}
	     catch (SQLException e) {
	         e.printStackTrace();
	         System.out.println("无法连接数据库");
	     }
	     catch(Exception e){
	         // 处理 Class.forName 错误
	         e.printStackTrace();
	         System.out.println("驱动器报错");
	       }
		return chosed;
	}

}
